package com.example.chat.controller;

import com.example.chat.domain.dto.VerifyForm;
import com.example.chat.result.Result;
import com.example.chat.service.FriendshipService;
import com.example.chat.utils.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/friends")
@CrossOrigin
@Slf4j
@Tag(name = "用户关系相关接口")
public class FriendshipController {

    @Resource
    private FriendshipService friendshipService;

    @Operation(summary = "根据手机号查询")
    @GetMapping("/byPhone")
    public Result getByPhone(@RequestParam(value = "phone") String phone){
        return friendshipService.getByPhone(phone);
    }

    @Operation(summary = "根据学号查询")
    @GetMapping("/byStudentId")
    public Result byStudentId(@RequestParam(value = "studentId") String StudentId){
        return friendshipService.byStudentId(StudentId);
    }

    @Operation(summary = "发送好友申请")
    @PostMapping("/add/{id}")
    public Result addFriend(@PathVariable Long id){
        log.info("ID为{}的用户对ID为{}发送好友申请", UserHolder.getCurrentId(),id);
        return friendshipService.addFriend(id);
    }

    @Operation(summary = "好友申请列表")
    @GetMapping("/newfriends")
    public Result getNewFriendList(){
        return friendshipService.getNewFriendList();
    }

    @Operation(summary = "同意或忽略")
    @PutMapping
    public Result passOrIgnore(@RequestBody VerifyForm verifyForm){
        String s = verifyForm.getType() == 1 ? "通过了" : "拒绝了";
        log.info("ID为{}的用户{}ID为{}的好友申请", UserHolder.getCurrentId(),s,verifyForm.getId());
        return friendshipService.passOrIgnore(verifyForm);
    }

    @Operation(summary = "好友申请个数")
    @GetMapping("/verifyCount")
    public Result getVerifyCount(){
        return friendshipService.getVerifyCount();
    }

    @Operation(summary = "查询好友列表")
    @GetMapping("/list")
    public Result getFriendsList(){
        return friendshipService.getFriendsList();
    }

    @Operation(summary = "根据id删除")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        log.info("ID为{}的用户删除了好友{}", UserHolder.getCurrentId(),id);
        return friendshipService.deleteById(id);
    }

    @Operation(summary = "查询可能认识用户")
    @GetMapping("/maybeKnow")
    public Result maybeKnow(){
        return friendshipService.maybeKnow();
    }
}
